﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Device
{
    public partial  class DeviceMaster
    {
        public enum AuthResult
        {
            Register, SerialNumnotNull, NumDoesnotMatch, EmailExist, Login, PasswordDoesnotMatch, Success, DoesnotMatch
        }
        
        //注册时根据用户输入的序列号进行相应的判断
        public static AuthResult add(string serialnum,string email)
        {
            using (Model1Container db=new Model1Container ())
            {
                Equiment sh=new Equiment ();
                List <Equiment > mun=(from u in db .Equiment集 where u.serialnumber ==serialnum select u ).ToList ();
                if(mun.Count ()>0)
                {
                    List<Equiment > notnull=(from u in db.Equiment集 where u.serialnumber ==serialnum && u.ownerid !=null  select u).ToList ();
                    if(notnull .Count ()>0)
                    {
                        return AuthResult .SerialNumnotNull ;
                    }
                    else 
                    {
                        //判断注册邮箱是否重复
                        sh =db .Equiment集 .FirstOrDefault (cc =>cc .serialnumber ==serialnum );
                        var ownerid=sh .ownerid ;
                        List <EquipmentMaster > emails=(from u in db .EquipmentMaster集 where u .id ==ownerid && u .email !=null select u).ToList ();
                       if(emails .Count ()>0)
                       {
                           return  AuthResult.EmailExist ;
                       }
                        else 
                       {
                           return  AuthResult.Register ;
                       }
                    }
                }
                else
                {
                    return  AuthResult.NumDoesnotMatch ;
                }
            }
        }


        //判断邮箱和密码是否匹配
        public static AuthResult auth(string email, string password)
        {
            using (Model1Container db = new Model1Container())
            {
                EquipmentMaster  sh = new EquipmentMaster ();
                List<EquipmentMaster > match = (from u in db.EquipmentMaster集  where u.email == email && u.password == password select u).ToList();
                if (match.Count() > 0)
                {
                    return AuthResult.Login ;
                }
                else
                {
                    return AuthResult.PasswordDoesnotMatch;
                }
            }
        }


        //注册用户信息
       public static void UserRegister(string username, string password,string email,string name,string answer,string note,string question)
        {
            using (Model1Container db = new Model1Container())
            {
                var  password_sha = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                EquipmentMaster sh = new EquipmentMaster();
                //需要生成url名吗
                sh.username = username;
                sh.password = password_sha;
                sh.email = email;
                sh.name = name;
                sh.answer = answer;
                sh.note = note;
                sh.question = question;               
                db.EquipmentMaster集.AddObject(sh);
                db.SaveChanges();               
            }
        }


       //注册设备
       public static void DeviceRegister(string serialnumber,string email)
       {                  
           using (Model1Container db = new Model1Container())
           {
               EquipmentMaster sh = new EquipmentMaster();
               Equiment ch = new Equiment();
               sh = db.EquipmentMaster集.FirstOrDefault(cc => cc.email == email);
               var ownerid = sh.id;
               ch.serialnumber = serialnumber;
               ch.ownerid = ownerid;
               db.Equiment集.AddObject(ch);
               db.SaveChanges();               
           }
       }


        //查询用户id
       public static Int32 CheckUserId ( string email)
       {
           using (Model1Container db = new Model1Container())
           {
               EquipmentMaster sh = new EquipmentMaster();
               Equiment ch = new Equiment();
               sh = db.EquipmentMaster集.FirstOrDefault(cc => cc.email == email);
               var ownerid = sh.id;
               return ownerid;
           }
       }



       //找回密码时校验用户、问题、答案
       public static AuthResult queryUserQueAnser(string email, string question, string answer)
       {
           using (Model1Container db = new Model1Container())
           {
               EquipmentMaster cm = new EquipmentMaster();
               List<EquipmentMaster> sheb = (from u in db.EquipmentMaster集 where u.email == email && u.question == question && u.answer == answer select u).ToList();
               if (sheb.Count() > 0)
               {
                   return AuthResult.Success;
               }
               else
               {
                   return AuthResult.DoesnotMatch;
               }
           }
       }
    }
} 